package com.heima.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.pojo.TbVmType;
import com.heima.mapper.TbVmTypeMapper;
import com.heima.pojo.dto.TbVmTypeDto;
import com.heima.pojo.vo.PageResponse;
import com.heima.pojo.vo.TbVmTypeVo;
import com.heima.service.ITbVmTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 售货机类型表 服务实现类
 * </p>
 *
 * @author AkiRinko
 * @since 2024-06-09
 */
@Service
@RequiredArgsConstructor
public class TbVmTypeServiceImpl extends ServiceImpl<TbVmTypeMapper, TbVmType> implements ITbVmTypeService {
    private final TbVmTypeMapper tbVmTypeMapper;

    /**
     * 设备类型搜索
     *
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @Override
    public PageResponse<TbVmTypeVo> selectVmType(Integer pageIndex, Integer pageSize, String name) {
        LambdaQueryWrapper<TbVmType> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtil.isNotEmpty(name), TbVmType::getName, name);

        Page<TbVmType> page = new Page<>(pageIndex, pageSize);
        Page<TbVmType> result = tbVmTypeMapper.selectPage(page, wrapper);

        return PageResponse.of(result, TbVmTypeVo.class);
    }

    /**
     * 新增设别类型
     *
     * @param dto
     */
    @Override
    public void insertVmType(TbVmTypeDto dto) {
        TbVmType tbVmType = BeanUtil.copyProperties(dto, TbVmType.class);
        tbVmTypeMapper.insert(tbVmType);
    }

    /**
     * 修改设备类型
     *
     * @param typeId
     * @param dto
     */
    @Override
    public void updateVmType(Integer typeId, TbVmTypeDto dto) {
        TbVmType tbVmType = BeanUtil.copyProperties(dto, TbVmType.class);

        LambdaQueryWrapper<TbVmType> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbVmType::getTypeId, typeId);

        tbVmTypeMapper.update(tbVmType, wrapper);
    }

    /**
     * 删除设备类型
     *
     * @param typeId
     */
    @Override
    public void deleteVmType(Integer typeId) {
        tbVmTypeMapper.deleteById(typeId);
    }
}
